<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="原编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入原编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排水点位置" prop="psdInstallation">
        <el-input
          v-model="queryParams.psdInstallation"
          placeholder="请输入排水点位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="水泵名称" prop="sbName">
        <el-input
          v-model="queryParams.sbName"
          placeholder="请输入水泵名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="水泵型号" prop="sbModel">
        <el-input
          v-model="queryParams.sbModel"
          placeholder="请输入水泵型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:drainageDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:drainageDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:drainageDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="drainageDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark"  width="150"/>
      <el-table-column
        label="排水点位置"
        align="center"
        prop="psdInstallation"
         width="150"
      />
      <el-table-column label="覆盖范围" align="center" prop="fgScope"  width="150"/>
      <el-table-column label="坐标X" align="center" prop="sbX"  width="150"/>
      <el-table-column label="坐标Y" align="center" prop="sbY"  width="150"/>
      <el-table-column label="坐标Z" align="center" prop="sbZ"  width="150"/>
      <el-table-column label="泵房名称" align="center" prop="bfName"  width="150"/>
      <el-table-column label="水泵名称" align="center" prop="sbName"  width="150"/>
      <el-table-column label="水泵效率" align="center" prop="sbXl"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.sbXl }} <span class="price">%</span></span>
        </template>
      </el-table-column>
      <el-table-column label="水泵额定流量" align="center" prop="sbEdll"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.sbEdll }} <span class="price">m³/h</span></span>
        </template>
      </el-table-column>
      <el-table-column label="水泵型号" align="center" prop="sbModel"  width="150"/>
      <el-table-column
        label="数据定义时间"
        align="center"
        prop="dataTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
         width="150"
         fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:drainageDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:drainageDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排水量测点基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="原编码" prop="cdCode">
          <el-input v-model="form.cdCode" placeholder="请输入原编码" />
        </el-form-item>
        <el-form-item label="排水点位置" prop="psdInstallation">
          <el-input
            v-model="form.psdInstallation"
            placeholder="请输入排水点位置"
          />
        </el-form-item>
        <el-form-item label="覆盖范围" prop="fgScope">
          <el-input v-model="form.fgScope" placeholder="请输入覆盖范围" />
        </el-form-item>
        <el-form-item label="坐标X" prop="sbX">
          <el-input v-model="form.sbX" placeholder="请输入坐标X" />
        </el-form-item>
        <el-form-item label="坐标Y" prop="sbY">
          <el-input v-model="form.sbY" placeholder="请输入坐标Y" />
        </el-form-item>
        <el-form-item label="坐标Z" prop="sbZ">
          <el-input v-model="form.sbZ" placeholder="请输入坐标Z" />
        </el-form-item>
        <el-form-item label="泵房名称" prop="bfName">
          <el-input v-model="form.bfName" placeholder="请输入泵房名称" />
        </el-form-item>
        <el-form-item label="水泵名称" prop="sbName">
          <el-input v-model="form.sbName" placeholder="请输入水泵名称" />
        </el-form-item>
        <el-form-item label="水泵效率" prop="sbXl">
          <el-input
            v-model="form.sbXl"
            placeholder="请输入水泵效率"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">%</span>
        </el-form-item>
        <el-form-item label="水泵额定流量" prop="sbEdll">
          <el-input
            v-model="form.sbEdll"
            placeholder="请输入水泵额定流量"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">m³/h</span>
        </el-form-item>
        <el-form-item label="水泵型号" prop="sbModel">
          <el-input v-model="form.sbModel" placeholder="请输入水泵型号" />
        </el-form-item>
        <el-form-item label="数据定义时间" prop="dataTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.dataTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择数据定义时间"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDrainageDefine,
  getDrainageDefine,
  delDrainageDefine,
  addDrainageDefine,
  updateDrainageDefine
} from "@/api/define/drainageDefine";

export default {
  name: "DrainageDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排水量测点基础信息表格数据
      drainageDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据定义时间时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        psdInstallation: null,

        fgScope: null,

        sbX: null,

        sbY: null,

        sbZ: null,

        bfName: null,

        sbName: null,

        sbXl: null,

        sbEdll: null,

        sbModel: null,

        dataTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],

        psdInstallation: [
          { required: true, message: "排水点位置不能为空", trigger: "blur" }
        ],

        fgScope: [
          { required: true, message: "覆盖范围不能为空", trigger: "blur" }
        ],

        sbX: [{ required: true, message: "坐标X不能为空", trigger: "blur" }],

        sbY: [{ required: true, message: "坐标Y不能为空", trigger: "blur" }],

        sbZ: [{ required: true, message: "坐标Z不能为空", trigger: "blur" }],

        bfName: [
          { required: true, message: "泵房名称不能为空", trigger: "blur" }
        ],

        sbName: [
          { required: true, message: "水泵名称不能为空", trigger: "blur" }
        ],

        sbXl: [
          { required: true, message: "水泵效率不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sbEdll: [
          { required: true, message: "水泵额定流量不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sbModel: [
          { required: true, message: "水泵型号不能为空", trigger: "blur" }
        ],

        dataTime: [
          { required: true, message: "数据定义时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排水量测点基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDataTime && "" != this.daterangeDataTime) {
        this.queryParams.params["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams.params["endDataTime"] = this.daterangeDataTime[1];
      }
      listDrainageDefine(this.queryParams).then(response => {
        this.drainageDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        psdInstallation: null,

        fgScope: null,

        sbX: null,

        sbY: null,

        sbZ: null,

        bfName: null,

        sbName: null,

        sbXl: null,

        sbEdll: null,

        sbModel: null,

        dataTime: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.sbXl = this.form.sbXl.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.sbEdll = this.form.sbEdll.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排水量测点基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDrainageDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排水量测点基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrainageDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrainageDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delDrainageDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
